﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

namespace Phoenix.Backend
{
    public partial class PortShippingSchedule : System.Web.UI.Page
    {
        protected DataTable dt;
        DataTable dtPort;
        Phoenix.AppCode.Entities.PortShippingSchedule portshippingschedule = new Phoenix.AppCode.Entities.PortShippingSchedule();
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'/>" + "Cập nhật thông tin thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'/>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'/>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    // load drop down list TrackingEList type
                    dtPort = Phoenix.AppCode.Bussiness.PortService.PortList();

                    ddlPort.DataSource = dtPort;
                    ddlPort.DataTextField = "PortName";
                    ddlPort.DataValueField = "ID_Port";
                    ddlPort.DataBind();
                    //add select dropdownlist
                    ddlPort.Items.Insert(0, new ListItem("Xem tất cả", "0"));

                    if (Request.Cookies["ddlPort"] == null)
                    {
                        Response.Cookies["ddlPort"].Value = "0";
                        Response.Cookies["ddlPort"].Expires = DateTime.Now.AddDays(1);
                    }
                    dt = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingScheduleListById(Convert.ToInt32(Request.Cookies["ddlPort"].Value.Trim()));

                    ddlPort.SelectedValue = Request.Cookies["ddlPort"].Value;

                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "OnOff" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = true;
                        edit.Visible = true;
                        Load_ActionTrackingEList(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action TrackingEList
        private void Load_ActionTrackingEList(string action)
        {
            //panelCreateCategory.Visible = visible;
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = false;

                    // load drop down list TrackingEList type
                    dtPort = Phoenix.AppCode.Bussiness.PortService.PortList();

                    ddlPortA.DataSource = dtPort;
                    ddlPortA.DataTextField = "PortName";
                    ddlPortA.DataValueField = "ID_Port";
                    ddlPortA.DataBind();

                    ddlPortA.SelectedValue = Request.Cookies["ddlPort"].Value;
                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = false;

                    // load drop down list TrackingEList type
                    dtPort = Phoenix.AppCode.Bussiness.PortService.PortList();

                    ddlPortE.DataSource = dtPort;
                    ddlPortE.DataTextField = "PortName";
                    ddlPortE.DataValueField = "ID_Port";
                    ddlPortE.DataBind();

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingScheduleList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    ddlPortE.SelectedValue = dt_Edit.Rows[0]["ID_Port"].ToString();
                    txtFEEDERE.Text = dt_Edit.Rows[0]["FEEDER"].ToString();
                    txtVOYE.Text = dt_Edit.Rows[0]["VOY"].ToString();
                    txtETDHCME.Text = DateTime.Parse(dt_Edit.Rows[0]["ETDHCM"].ToString()).ToString("yyyy/MM/dd");
                    txtCONNECTINGVESSELE.Text = dt_Edit.Rows[0]["CONNECTINGVESSEL"].ToString();
                    txtETDE.Text = dt_Edit.Rows[0]["ETD"].ToString();
                    txtETADESTE.Text = DateTime.Parse(dt_Edit.Rows[0]["ETADEST"].ToString()).ToString("yyyy/MM/dd");
                }
                else if (action == "Delete")
                {
                    try
                    {
                        portshippingschedule = Phoenix.AppCode.Bussiness.PortShippingScheduleService.GetPortShippingScheduleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.PortShippingScheduleService.Delete(portshippingschedule);
                        lblWarning.Text = success;
                        //Response.Redirect("PortShippingSchedule.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else if (action == "OnOff")
                {
                    try
                    {
                        portshippingschedule = Phoenix.AppCode.Bussiness.PortShippingScheduleService.GetPortShippingScheduleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.PortShippingScheduleService.OnOff(portshippingschedule);
                        lblWarning.Text = success;
                        //Response.Redirect("PortShippingSchedule.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                portshippingschedule.ID_Port = Convert.ToInt32(ddlPortA.SelectedValue);
                portshippingschedule.FEEDER = txtFEEDERA.Text; //DateTime.Now.ToString("G", CultureInfo.CreateSpecificCulture("en-US"))
                portshippingschedule.VOY = txtVOYA.Text;
                portshippingschedule.ETDHCM = Convert.ToDateTime(txtETDHCMA.Text);
                portshippingschedule.CONNECTINGVESSEL = txtCONNECTINGVESSELA.Text;
                portshippingschedule.ETD = txtETDA.Text;
                portshippingschedule.ETADEST = Convert.ToDateTime(txtETADESTA.Text); 
                portshippingschedule.PostUser = Session["UserName"].ToString();
                Phoenix.AppCode.Bussiness.PortShippingScheduleService.Create(portshippingschedule);

                ClearAllFields();
                lblWarningA.Text = success;
                //Response.Redirect("PortShippingSchedule.aspx?Action=Create");
            }
            catch
            {
                lblWarningA.Text = error;
                //Response.Redirect("Error.aspx");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarningA.Text = info;
        }

        private void ClearAllFields()
        {
            txtFEEDERA.Text = "";
            txtVOYA.Text = "";
            txtETDHCMA.Text = "";
            txtCONNECTINGVESSELA.Text = "";
            txtETDA.Text = "";
            txtETADESTA.Text = "";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {

            portshippingschedule = Phoenix.AppCode.Bussiness.PortShippingScheduleService.GetPortShippingScheduleByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            portshippingschedule.ID_Port = Convert.ToInt32(ddlPortE.SelectedValue);
            portshippingschedule.FEEDER = txtFEEDERE.Text; //DateTime.Now.ToString("G", CultureInfo.CreateSpecificCulture("en-US"))
            portshippingschedule.VOY = txtVOYE.Text;
            portshippingschedule.ETDHCM = Convert.ToDateTime(txtETDHCME.Text);
            portshippingschedule.CONNECTINGVESSEL = txtCONNECTINGVESSELE.Text;
            portshippingschedule.ETD = txtETDE.Text;
            portshippingschedule.ETADEST = Convert.ToDateTime(txtETADESTE.Text);
            portshippingschedule.EditUser = Session["UserName"].ToString();

            Phoenix.AppCode.Bussiness.PortShippingScheduleService.Edit(portshippingschedule);
            lblWarningE.Text = success;
            //Response.Redirect("PortShippingSchedule.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");

        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingScheduleList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            ddlPortE.SelectedValue = dt_Edit.Rows[0]["ID_Port"].ToString();
            txtFEEDERE.Text = dt_Edit.Rows[0]["FEEDER"].ToString();
            txtVOYE.Text = dt_Edit.Rows[0]["VOY"].ToString();
            txtETDHCME.Text = DateTime.Parse(dt_Edit.Rows[0]["ETDHCM"].ToString()).ToString("yyyy/MM/dd");
            txtCONNECTINGVESSELE.Text = dt_Edit.Rows[0]["CONNECTINGVESSEL"].ToString();
            txtETDE.Text = dt_Edit.Rows[0]["ETD"].ToString();
            txtETADESTE.Text = DateTime.Parse(dt_Edit.Rows[0]["ETADEST"].ToString()).ToString("yyyy/MM/dd");
            lblWarningE.Text = info;
        }

        protected void ddlPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingScheduleListById(Convert.ToInt32(ddlPort.SelectedValue));
            Response.Cookies["ddlPort"].Value = ddlPort.SelectedValue;
        }
    }
}